<template>
  <div class="system-menuset-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <!-- <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <!-- <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel> 
      default-expand-all
      -->
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      row-key="RouteID"
      :data="list"
      style="width: 100%"
      border
      lazy
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- <el-table-column prop="RouteID" label="菜单id" width="80" /> -->
      <el-table-column prop="title" label="菜单名称" width="220" />
      <el-table-column align="center" prop="path" label="path" width="130" />
      <el-table-column align="center" prop="name" label="name" width="130" />
      <el-table-column prop="component" label="component" width="200" />
      <el-table-column prop="icon" label="icon" width="180" />
      <el-table-column align="center" label="isSubRoute" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.isSubRoute"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'isSubRoute')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="breadcrumbHidden">
        <template #default="{ row }">
          <el-switch
            v-model="row.breadcrumbHidden"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'breadcrumbHidden')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="tabHidden">
        <template #default="{ row }">
          <el-switch
            v-model="row.tabHidden"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'tabHidden')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="hidden" width="80">
        <template #default="{ row }">
          <el-switch
            v-model="row.hidden"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'hidden')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="noClosable">
        <template #default="{ row }">
          <el-switch
            v-model="row.noClosable"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'noClosable')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="dot" width="80">
        <template #default="{ row }">
          <el-switch
            v-model="row.dot"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'dot')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="state" width="80">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @change="changeState(row, 'state')"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="90">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <edit ref="editRef" :list="list" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doState } from '@/api/systemMenuset'
  import Edit from './components/SystemMenusetEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'SystemMenuset',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
        expands: [], //要展开的行，数值的元素是row的key值
        menuoptions: [],
      })
      //获取row的key值
      const getRowKeys = (row) => {
        return row.RouteID
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const changeState = (row, type) => {
        let stateMsg = ''
        if (
          row.breadcrumbHidden == 0 ||
          row.tabHidden == 0 ||
          row.hidden == 0 ||
          row.isSubRoute == 0 ||
          row.noClosable == 0 ||
          row.dot == 0 ||
          row.state == 0
        )
          stateMsg = '你确定要设置吗？'
        else if (
          row.breadcrumbHidden == 1 ||
          row.tabHidden == 1 ||
          row.hidden == 1 ||
          row.isSubRoute == 1 ||
          row.noClosable == 1 ||
          row.dot == 1 ||
          row.state == 1
        )
          stateMsg = '你确定要设置吗？'
        $baseConfirm(stateMsg, null, async () => {
          if (type == 'breadcrumbHidden') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'breadcrumbHidden', //操作的字段名
              ActValue: row.breadcrumbHidden, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          } else if (type == 'tabHidden') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'tabHidden', //操作的字段名
              ActValue: row.tabHidden, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          } else if (type == 'hidden') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'hidden', //操作的字段名
              ActValue: row.hidden, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          } else if (type == 'isSubRoute') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'isSubRoute', //操作的字段名
              ActValue: row.isSubRoute, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          } else if (type == 'noClosable') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'noClosable', //操作的字段名
              ActValue: row.noClosable, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          } else if (type == 'dot') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'dot', //操作的字段名
              ActValue: row.dot, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          } else if (type == 'state') {
            const setState = {
              RouteID: row.RouteID, //操作的ID
              ActField: 'state', //操作的字段名
              ActValue: row.state, //操作后的值
            }
            const { msg } = await doState(setState)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
          }
          await fetchData()
        })
      }
      const handleEdit = (row) => {
        if (row.RouteID) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { routerList },
        } = await getList(state.queryForm)
        state.list = routerList
        // state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        getRowKeys,
        setSelectRows,
        changeState,
        handleEdit,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
